package com.aaa.sercurity;

import com.aaa.dao.F_userDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.F_user;
import com.aaa.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * jwt 增强类 存放用户人信息
 */
@Component
public class JwtEnhanse implements TokenEnhancer {

    @Resource
            @Autowired
    UserDao userDao;

    @Resource
    UserDetailsConfig userDetailsConfig;

    @Resource
    F_userDao fuserDao;
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
        if(userDetailsConfig.cid.equals("vueback")) {
            Map<String, Object> hashMap = new HashMap<>();
            List<User> list = userDao.selectByUname(userDetailsConfig.a);
            System.out.println("a:" + userDetailsConfig.a);
            System.out.println("这是后台登陆人信息" + list);
            hashMap.put("list", list.get(0));
            DefaultOAuth2AccessToken defaultOAuth2AccessToken = (DefaultOAuth2AccessToken) oAuth2AccessToken;
            defaultOAuth2AccessToken.setAdditionalInformation(hashMap);
            return defaultOAuth2AccessToken;
        }else if(userDetailsConfig.cid.equals("vuefront")){
            Map<String, Object> hashMap1 = new HashMap<>();
            List<F_user> list1 =  fuserDao.selectByUname(userDetailsConfig.a);
            System.out.println("a:"+userDetailsConfig.a);
            System.out.println("这是前台登陆人信息"+list1);
            hashMap1.put("list",list1.get(0));
            DefaultOAuth2AccessToken defaultOAuth2AccessToken1= (DefaultOAuth2AccessToken) oAuth2AccessToken;
            defaultOAuth2AccessToken1.setAdditionalInformation(hashMap1);
            return defaultOAuth2AccessToken1;
        }else{
            return null;
        }

    }
}
